﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Leyp.Components.Module;
using System.Collections.Generic;
using Leyp.SQLServerDAL;
using Leyp.Model;
using Leyp.BLL;

public partial class Index : PageBase
{
    protected enum TitleEuem
    { 
        gkxx,
        xtsz,
        cgkd,
        xskd,
        kckd,
        gllc
    }
    private int count = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Databang();
        }
    }
    /// <summary>
    /// 根据权限初始化用户菜单
    /// </summary>
    protected void Databang()
    {
        AuthorityDAL bAuthority = Leyp.SQLServerDAL.Factory.getAuthorityDAL();

        List<GroupAuthority> AL = new List<GroupAuthority>();
        List<Authority> AL0 = new List<Authority>();//系统设置 0
        List<Authority> AL1 = new List<Authority>();//采购开单 1
        List<Authority> AL2 = new List<Authority>();//销售开单 2
        List<Authority> AL3 = new List<Authority>();//库存开单 3
        List<Authority> AL4 = new List<Authority>();//管理流程模块 4

        AL = Leyp.SQLServerDAL.Factory.getGroupAuthorityDAL().getALLGroupAuthorityByGroupID(getGroupID());

        for (int i = 0; i < AL.Count; i++)
        {
            GroupAuthority g = AL[i];
            Authority a = bAuthority.getByID(g.AuthorityID);
            if (a.TypeID == 0)
            {
                AL0.Add(a);
            }
            else if (a.TypeID == 1)
            {
                AL1.Add(a);
            }
            else if (a.TypeID == 2)
            {
                AL2.Add(a);

            }
            else if (a.TypeID == 3)
            {
                AL3.Add(a);
            }
            else if (a.TypeID == 4)//工作流
            {
                AL4.Add(a);
            }
        }


        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Columns.Add("Title");
        table.Columns.Add("Type");

        int id = 0;
        DataRow row = table.NewRow();
        row["ID"] = id;
        row["Title"] = "公开信息";
        row["Type"]=(int)TitleEuem.gkxx;
        id++;
        table.Rows.Add(row);



        if (AL1.Count > 0)
        {
             row = table.NewRow();
            row["Type"] = (int)TitleEuem.xtsz;
            row["Title"] = "采购开单";
            row["ID"] = id;
            id++;
            table.Rows.Add(row);
        }

        if (AL2.Count > 0)
        {
             row = table.NewRow();
            row["Type"] = (int)TitleEuem.xskd;
            row["Title"] = "销售开单";
            row["ID"] = id;
            id++;
            table.Rows.Add(row);
        }

        if (AL3.Count > 0)
        {
             row = table.NewRow();
            row["Type"] = (int)TitleEuem.kckd;
            row["Title"] = "库存开单";
            row["ID"] = id;
            id++;
            table.Rows.Add(row);
        }

        if (AL4.Count > 0)
        {
             row = table.NewRow();
            row["Type"] = (int)TitleEuem.gllc;
            row["Title"] = "管理流程";
            row["ID"] = id;
            table.Rows.Add(row);
            id++;
        }

        if (AL0.Count > 0)
        {
            row = table.NewRow();
            row["Type"] = (int)TitleEuem.xtsz;
            row["Title"] = "系统设置";
            row["ID"] = id;
            id++;
            table.Rows.Add(row);
        }

        count = id;
        TitleRepeater.DataSource = table;
        TitleRepeater.DataBind();
    }
    protected void TitleRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Repeater ItemRepeater = (Repeater)e.Item.FindControl("ItemRepeater"); 

        DataRowView rowv = (DataRowView)e.Item.DataItem;


        AuthorityDAL bAuthority = Leyp.SQLServerDAL.Factory.getAuthorityDAL();

        List<GroupAuthority> AL = new List<GroupAuthority>();
        List<Authority> AL0 = new List<Authority>();//系统设置 0
        List<Authority> AL1 = new List<Authority>();//采购开单 1
        List<Authority> AL2 = new List<Authority>();//销售开单 2
        List<Authority> AL3 = new List<Authority>();//库存开单 3
        List<Authority> AL4 = new List<Authority>();//管理流程模块 4

        AL = Leyp.SQLServerDAL.Factory.getGroupAuthorityDAL().getALLGroupAuthorityByGroupID(getGroupID());

        for (int i = 0; i < AL.Count; i++)
        {
            GroupAuthority g = AL[i];
            Authority a = bAuthority.getByID(g.AuthorityID);
            if (a.TypeID == 0)
            {
                AL0.Add(a);
            }
            else if (a.TypeID == 1)
            {
                AL1.Add(a);
            }
            else if (a.TypeID == 2)
            {
                AL2.Add(a);

            }
            else if (a.TypeID == 3)
            {
                AL3.Add(a);
            }
            else if (a.TypeID == 4)//工作流
            {
                AL4.Add(a);
            }
        }
        int type = Convert.ToInt32(rowv["Type"]); 
        switch(type)
        {
            case (int)TitleEuem.gkxx:///公开信息 -1
                DataTable talbe = new DataTable();
                talbe.Columns.Add("WebUrl");
                talbe.Columns.Add("AuthorityName");

                DataRow row = talbe.NewRow();
                row["WebUrl"] = "Products/NewsProducts.aspx";
                row["AuthorityName"] = "最新商品";
                talbe.Rows.Add(row);

                row = talbe.NewRow();
                row["WebUrl"] = "Products/ShowProductsList.aspx";
                row["AuthorityName"] = "物品搜索";
                talbe.Rows.Add(row);

                ItemRepeater.DataSource = talbe;
                ItemRepeater.DataBind();  ///
                break;
            case (int)TitleEuem.xtsz:///系统设置0
                ItemRepeater.DataSource = AL0;
                ItemRepeater.DataBind();                
                break;
            case (int)TitleEuem.cgkd:///采购开单1
                ItemRepeater.DataSource = AL1;
                ItemRepeater.DataBind();  ///
                break;
            case (int)TitleEuem.xskd:///销售开单2
                ItemRepeater.DataSource = AL2;
                ItemRepeater.DataBind();  ///
                break;
            case (int)TitleEuem.kckd:///库存开单3
                ItemRepeater.DataSource = AL3;
                ItemRepeater.DataBind();  ///
                break;
            case (int)TitleEuem.gllc:///管理流程4
                ItemRepeater.DataSource = AL4;
                ItemRepeater.DataBind();  ///
                break;
        }
    }
    protected int GetCount()
    {
        return count;
    }
}
